<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../assets/css/layui.css" media="all">
</head>
<body class="childrenBody">

<form class="layui-form">
    <table id="test" lay-filter="userList"></table>

    <!--操作-->
    <script type="text/html" id="userListBar">



    </script>
</form>
<script src="../assets/layui.js" charset="utf-8"></script>
<script>
    layui.use(['table','jquery','form'], function(){
        var table = layui.table;
        var $ = layui.jquery,form = layui.form;

        var tableIns;
        var p3 = "\<\script type=\"text/html\" id=\"toolbarDemo\"\>\n" +
            "\n" +
            "        <div class=\"layui-col-sm12\">\n" +
            "            <div class=\"layui-inline\">\n" +
            "                <div class=\"layui-input-inline\">\n" +
            "                    <input id=\"key\" type=\"text\" class=\"layui-input searchVal\" placeholder=\"请输入搜索的内容\" />\n" +
            "                </div>\n" +
            "                <a class=\"layui-btn search_btn\" data-type=\"reload\" lay-event=\"search\">搜索</a>\n" +
            "            </div>\n" +
            "           \n" +
            "        </div>\n" +
            "\n" +
            "   \<\/script\>"
        var p12 ="\<script type=\"text/html\" id=\"toolbarDemo\"\>\n" +
            "\n" +
            "        <div class=\"layui-col-sm12\">\n" +
            "            <div class=\"layui-inline\">\n" +
            "                <div class=\"layui-input-inline\">\n" +
            "                    <input id=\"key\" type=\"text\" class=\"layui-input searchVal\" placeholder=\"请输入搜索的内容\" />\n" +
            "                </div>\n" +
            "                <a class=\"layui-btn search_btn\" data-type=\"reload\" lay-event=\"search\">搜索</a>\n" +
            "            </div>\n" +
            "            <div class=\"layui-inline\">\n" +
            "                <a id=\"add\" class=\"layui-btn layui-btn-normal addNews_btn\" lay-event=\"add\">添加活动</a>\n" +
            "            </div>\n" +
            "        </div>\n" +
            "\n" +
            "    \<\/script\>";
        var edit = " <a class=\"layui-btn layui-btn-xs\" lay-event=\"edit\">编辑</a>";
        var  show = "<a class=\"layui-btn layui-btn-xs layui-btn-normal\" lay-event=\"edit\">查看</a>";
        var del = " <a class=\"layui-btn layui-btn-xs layui-btn-danger\" lay-event=\"del\">删除</a>";
        $.ajax({
            url: '../user.do',
            data: {
                'tran':'select',
                'get':'info',
                'id':'self'
            },
            dataType: 'json',
            type: 'post',
            async: false,
            success: function(data) {
                if (data.permissions<='1'){
                    $(".layui-form").append(p12);
                    $("#userListBar").append(edit);
                    $("#userListBar").append(show);
                    $("#userListBar").append(del);
                }else {
                    $(".layui-form").append(p3);

                    $("#userListBar").append(show);
                }
            }
        });

        function flush() {
           tableIns =  table.render({
                elem: '#test'
                ,url:'../activity.do'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,title: '活动数据表'
                ,method:'post'
                ,where:{
                    tran:'list'
                }
                ,cols: [
                    [{type: 'checkbox', fixed: 'left'}
                        ,{
                        field: 'id',
                        width: 80,
                        title: '编号',
                        sort: true
                    }, {
                        field: 'name',
                        width: 120,
                        title: '活动名'
                    }, {
                        field: 'holdingDay',
                        width: 120,
                        title: '举办时间'
                    }, {
                        field: 'submittedDay',
                        width: 120,
                        title: '创建时间',
                    }, {
                        field: 'passDay',
                        width: 120,
                        title: '审核时间'
                    }, {
                        field: 'subDepName',
                        title: '提交部门',
                        width:150
                    }, {
                        field: 'subUsername',
                        width: 100,
                        title: '提交人',
                        sort: true
                    },{
                        field: 'depName',
                        width: 150,
                        title: '抄送部门',
                        sort: true,
                    },{
                        field: 'distribute',
                        width: 120,
                        title: '状态',
                        templet:function(a){
                            if (a.distribute == "0"){
                                return "通过";
                            }else if (a.distribute == "1"){
                                return "审核中";
                            }else if(a.distribute == "2"){
                                return "拒绝";
                            }else if(a.distribute == "3"){
                                return "安排人员";
                            }
                        }
                    },{fixed: 'right', title:'操作', toolbar: '#userListBar', width:180}]
                ],
                page: true
            });
        }
        flush();

        //头工具栏事件
        table.on('toolbar(userList)', function(obj){

            switch(obj.event){
                case 'add':
                    var index = layui.layer.open({
                        title : "添加活动",
                        type : 2,
                        content : "activityadd.html",
                        success : function(layero, index){
                            var body = layui.layer.getChildFrame('body', index);
                            body.find(".layui-btn.layui-btn1").click(function () {

                                setTimeout(function(){
                                    layui.layer.tips('点击此处返回活动列表', '.layui-layer-setwin .layui-layer-close', {
                                        tips: 3
                                    });
                                },500)

                            });
                            setTimeout(function(){
                                layui.layer.tips('点击此处返回活动列表', '.layui-layer-setwin .layui-layer-close', {
                                    tips: 3
                                });
                            },500)
                        }
                    })
                    layui.layer.full(index);
                    window.sessionStorage.setItem("index",index);
                    var jquery = layui.jquery;
                    //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
                    jquery(window).on("resize",function(){
                        layui.layer.full(window.sessionStorage.getItem("index"));
                    })
                    break;
                case 'search':
                    if ($("#key").val() == ''){
                        flush();
                    }else {
                        tableIns.reload({
                            where:{
                                "seach":"queryActivity",
                                "key":$("#key").val()
                            }
                        });
                    }
                    break;
            };
        });


        //监听行工具事件
        table.on('tool(userList)', function(obj){
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    form.render();
                    $.ajax({
                        url: '../activity.do',
                        data: {
                            tran:'del',
                            id:obj.data.id
                        },
                        dataType: 'json',
                        type: 'post',
                        success: function(res) {
                            console.log(res);
                            if (res.code == '0') {
                                layer.msg(res.msg);
                                obj.del();
                            } else {
                                layer.msg('删除失败');
                            }

                        }
                    });

                    layer.close(index);
                });
            }
            else if(obj.event === 'edit'){
                var index = layui.layer.open({
                    title : "审核",
                    type : 2,
                    content : "activityInfo.html?id="+obj.data.id,
                    success : function(layero, index){
                        setTimeout(function(){
                            layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                                tips: 3
                            });
                        },500)
                    }
                })
                layui.layer.full(index);
                window.sessionStorage.setItem("index",index);
                var jquery = layui.jquery;
                //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
                jquery(window).on("resize",function(){
                    layui.layer.full(window.sessionStorage.getItem("index"));
                })
            }
        });
    });
</script>

</body>
</html>